package com.example.springboot.controller;

import com.example.springboot.entity.Device;
import com.example.springboot.service.DeviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/devices")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);

    /**
     * 获取设备状态
     * @param deviceId 设备ID
     * @return 设备状态
     */
    @GetMapping("/{deviceId}/status")
    public Device getDeviceStatus(@PathVariable Integer deviceId) {
        return deviceService.getDeviceStatus(deviceId);
    }

    /**
     * 根据 point_id 获取设备列表信息
     * @param pointId 设备点ID
     * @return 设备列表
     */
    @GetMapping("/point/{pointId}")
    public List<Device> getDevicesByPoint(@PathVariable Integer pointId) {
        return deviceService.getDevicesByPoint(pointId);
    }
    /**
     * 根据设备ID获取对应的自助点ID
     * @param deviceId 设备ID
     * @return 自助点ID
     */
    @GetMapping("/{deviceId}/point")
    public Integer getPointIdByDeviceId(@PathVariable Integer deviceId) {
        return deviceService.getPointIdByDeviceId(deviceId);
    }
    @GetMapping
    public ResponseEntity<List<Device>> getAllDevices(HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Fetching all devices for role: {}, username: {}", role, username);
        List<Device> devices = deviceService.getAllDevices(role, username);
        return ResponseEntity.ok(devices);
    }

    @GetMapping("/{deviceId}")
    public ResponseEntity<Device> getDeviceById(
            @PathVariable Integer deviceId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Fetching device {} for role: {}, username: {}", deviceId, role, username);
        Device device = deviceService.getDeviceById(deviceId, role, username);
        return ResponseEntity.ok(device);
    }

    @PostMapping
    public ResponseEntity<Device> addDevice(
            @RequestBody Device device,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Creating new device for role: {}, username: {}", role, username);
        Device newDevice = deviceService.addDevice(device, role, username);
        return ResponseEntity.ok(newDevice);
    }

    @PutMapping("/{deviceId}")
    public ResponseEntity<Device> updateDevice(
            @PathVariable Integer deviceId,
            @RequestBody Device device,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Updating device {} for role: {}, username: {}", deviceId, role, username);
        Device updatedDevice = deviceService.updateDevice(deviceId, device, role, username);
        return ResponseEntity.ok(updatedDevice);
    }

    @DeleteMapping("/{deviceId}")
    public ResponseEntity<Void> deleteDevice(
            @PathVariable Integer deviceId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Deleting device {} for role: {}, username: {}", deviceId, role, username);
        deviceService.deleteDevice(deviceId, role, username);
        return ResponseEntity.noContent().build();
    }
}
